package com.jic.order.controller.manage;


import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.entity.OrderExamine;
import com.jic.order.manage.request.OrderExamineRequest;
import com.jic.order.manage.response.OrderExamineResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderExamineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "订单审核设置", tags = {"订单审核设置"})
@RequestMapping(value = "orderExamine")
@RestController
public class OrderExamineController {

    @Autowired
    OrderExamineService orderExamineService;

    /**
     * 分页查询
     * */
    @ApiOperation(value = "获取所有订单拆分信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<OrderExamineResponse>> queryPage(@RequestBody OrderExamineRequest form) {
        log.info("开始调用订单拆分分页，{}", JSON.toJSONString(form));
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        OrderExamine orderExamine = new OrderExamine();
        BeanUtils.copyProperties(form, orderExamine);
        DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
        if (deptType.getCode() != 0) {
            orderExamine.setDeptId(form.getDeptId());
        }
        PageResult<OrderExamine> result = orderExamineService.queryPage(orderExamine, page);
        List<OrderExamineResponse> orderExamineResponseList = result.getRows().stream().map(temp -> {
            OrderExamineResponse orderExamineResponse = new OrderExamineResponse();
            BeanUtils.copyProperties(temp, orderExamineResponse);
            return orderExamineResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<OrderExamineResponse> pageResult = new PageResult<>();
        pageResult.setRows(orderExamineResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }

    /**
     *  订单审核新增编辑
     *
     * */
    @ApiOperation(value = "订单审核新增编辑")
    @ResponseBody
    @PostMapping(value = "/editOrderExamine")
    public RestResult editOrderExamine(@RequestBody OrderExamineRequest orderExamineRequest) {
        log.info("开始调用订单审核新增编辑，{}", JSON.toJSONString(orderExamineRequest));
        RestResult restResult = new RestResult<>();
        try {
            OrderExamine orderExamine = new OrderExamine();
            BeanUtils.copyProperties(orderExamineRequest, orderExamine);
            orderExamineRequest.setDeptId(orderExamineRequest.getAdminOperate().getDeptId());
            orderExamineService.editOrderExamine(orderExamine);
            return RestResult.success(true);
        } catch (Exception e) {
            log.error("editOrderExamine========》", e);
        }
        return restResult;

    }


    /**
     *  删除订单审核设置
     *
     * */
    @ApiOperation(value = "删除订单审核设置")
    @ResponseBody
    @PostMapping(value = "/deleteOrderExamine")
    public RestResult deleteOrderExamine(@RequestBody OrderExamineRequest orderExamineRequest) {
        log.info("开始调用删除订单审核，{}", orderExamineRequest.toString());
        RestResult restResult = new RestResult<>();
        try {
            orderExamineService.delete(orderExamineRequest.getId());
            return RestResult.success(true);
        } catch (Exception e) {
            log.error("deleteOrderExamine========》", e);
        }
        return restResult;

    }

    /**
     *  批量停用订单审核
     *
     * */
    @ApiOperation(value = "批量停用订单审核")
    @ResponseBody
    @PostMapping(value = "/batchStatusOrderExamine")
    public RestResult batchStatusOrderExamine(@RequestBody OrderExamineRequest orderExamineRequest) {
        log.info("开始调用批量停用，{}", orderExamineRequest.toString());
        RestResult restResult = new RestResult<>();
        try {
            orderExamineService.batchStatusOrderExamine(orderExamineRequest);
            return RestResult.success(true);
        } catch (Exception e) {
            log.error("batchStatusOrderExamine========》", e);
            return RestResult.error("9999","批量停用操作失败");
        }

    }
}
